package com.xisoft.base.config.mybatis;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.xisoft.base.config.FieldMetaObjectHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * *    mybatis-plus 配置
 * Created with Description:
 *
 * @fileRelativePath: MybatisPlusConfig.java
 * @Date: Created on 2022/8/3  15:59  <br>
 * @author: LiBencheng <br>
 * @version: 1.0 <br>
 */
@Configuration      //拦截Executor类的query方法
public class MybatisPlusConfig
{
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor( )
    {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 分页插件
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 防止全表更新与删除
        mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        return mybatisPlusInterceptor;
    }

    @Bean
    public FieldMetaObjectHandler fieldMetaObjectHandler( )
    { return new FieldMetaObjectHandler(); }


    @Bean
    public ConfigurationCustomizer configurationCustomizer( )
    {
        return new ConfigurationCustomizer()
        {
            @Override
            public void customize(MybatisConfiguration configuration)
            {
                configuration.setObjectWrapperFactory(new MapWrapperFactory());
                configuration.setMapUnderscoreToCamelCase(false);
            }
        };
    }
}
